<%@ page contentType="text/html;charset=UTF-8"%>﻿
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1" name="viewport" />
<meta content="" name="description" />
<meta content="" name="author" />

<link rel="stylesheet" type="text/css"
	href="${ctx}/static/js/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css"
	href="${ctx}/static/js/themes/IconExtension.css" />

</head>
<body class="easyui-layout layout" style="background: #fff;">
	<input type="hidden" value="${ctx}" id="context_val">
	<div id="imgShow"
		style="position: absolute; display: none; border: 4px #ccc solid; z-index: 10; background-color: #fff; padding: 5px;">
		<img src="" width="150px">
	</div>
	<div region="north" border="false" title="工单搜索" collapsed="false"
		style="height: 80px; overflow: hidden; background: #eee; border-bottom: 1px solid #ddd"
		align="left">
		<form id="searchForm">
			<table style="height: 100%; width: auto;">
				<tr>
					<td>来电号码：</td>
					<td><input class="easyui-textbox" type="text"
						name="customerPhone" id="customerPhone"></input></td>
					<td>工单状态：</td>
					<td><select class="easyui-combobox" editable=false
						name="billStt" id="billStt">
							<option value="">--请选择--</option>
							<option value="0">待分派（需话务员处理）</option>
							<option value="6">待分派（需区域主管处理）</option>
							<option value="1">待处理</option>
							<option value="2">待回访</option>
							<option value="3">已完成</option>
							<option value="4">退单</option>
							<option value="9">作废</option>
					</select></td>
					<td>请求类型:</td>
					<td><select class="easyui-combobox" editable=false
						name="requestType" id="requestType">
							<option value="">--请选择--</option>
							<option value="0">维修</option>
							<option value="1">安装</option>
							<option value="2">投诉</option>
					</select></td>
					<td>紧急度:</td>
					<td><select value=1 class="easyui-combobox" editable=false
						name="emergentType" id="emergentType">
							<option value="">--请选择--</option>
							<option value="0">不紧急</option>
							<option value="1">一般</option>
							<option value="2">紧急</option>
					</select></td>
					<td align="center"><a href="javascript:void(0);"
						class="easyui-linkbutton" iconCls="icon-search"
						onclick="searchWorkBill();">查询</a></td>
				</tr>

			</table>
		</form>

	</div>
	<div region="center" border="false">

		<table id="data">  
			<tr>
				<td><span>输入查询条件进行工单查询。</span></td>
			</tr>
		</table>
		<div id="menu" class="easyui-menu" style="width: 80px; display: none;">
			<div onclick="viewData();" iconCls="icon-search">查看详情</div>
			<div onclick="assign();" iconCls="icon-20130410120031302_easyicon_net_16">分派</div>
			<div onclick="telephoneVisit();" iconCls="icon-redo">回访</div>
			<div>指定完成</div>
			<div>废单</div>
			
			
		</div>
		<div id="visit_dialog" class="easyui-dialog" style="padding:5px;width:500px;height:200px;"
        	title="回访框" iconCls="icon-ok"  buttons="#dlg-buttons"> 
	   		<form id="visit_form">
				<table >
					<tr>
						<td>工单号：</td>
						<td><input class="easyui-textbox" type="text"
							name="visit_billNo" id="visit_billNo"  disabled="true"></input></td>
						<td>来电号码：</td>
						<td><input class="easyui-textbox" type="text"
							name="visit_customerPhone" id="visit_customerPhone" disabled="true"></input></td>
					</tr>
					<tr>
					<td>问题描述:</td>
					<td>
					<textarea class="easyui-validatebox"  	name="visit_issueDesc" id="visit_issueDesc"  style="height:30px;" disabled="true"></textarea>
						<td>回访结果:</td>
					<td><select value=1 class="easyui-combobox" name="visit_result" id="visit_result">
							<option value="3" selected="true">用户满意</option>
							<option value="4">需重新派单</option>
					</select></td>
					

						</td>
					</tr>
					
				</table>
				</form>
		</div>  


		<div id="dlg-buttons">    
		    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="visitHander();">确定</a>    
		    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#visit_dialog').dialog('close')">取消</a>    
		</div>    
		

		
		<div id="assign_area_dialog" class="easyui-dialog" style="padding:5px;width:500px;height:200px;"
        	title="区域派工" iconCls="icon-ok"  buttons="#assign-buttons"> 
	   		<form id="assign_area_form">
				<table >
					<tr>
						<td>区域：</td>
						<td>
							<select  class="easyui-combobox" name="assign_area" id="assign_area">
							</select>
						</td>
							<td>主管:</td>
						<td>
						<select  class="easyui-combobox" name="area_emp" id="area_emp">
								
						</select>
					</td>	
						
					</tr>
					
				</table>
				</form>
		</div> 
		<div id="assign-buttons">    
		    <a href="#" class="easyui-linkbutton" iconCls="icon-20130410120031302_easyicon_net_16" onclick="area_AssignHander();">分派</a>    
		    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#assign_area_dialog').dialog('close')">取消</a>    
		</div>    
		
		
		<div id="assign_manager_dialog" class="easyui-dialog" style="padding:5px;width:500px;height:200px;"
        	title="主管派工" iconCls="icon-ok"  buttons="#assign-manager-buttons"> 
	   		<form id="assign_manager_form">
				<table >
					<tr>
						<td>所属主管：</td>
						<td>
							<input type="text" id="manager_id">
						</td>
							<td>接单人:</td>
						<td>
						<select  class="easyui-combobox" name="managed_emp" id="managed_emp">
								
						</select>
					</td>	
						
					</tr>
					
				</table>
				</form>
		</div> 
		<div id="assign-manager-buttons">    
		    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="manager_AssignHander();">分派</a>    
		    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#assign_manager_dialog').dialog('close')">取消</a>    
		</div>    
		
		
	</div>
	<script type="text/javascript"
		src="${ctx}/static/js/jquery-1.7.2.min.js"></script>
	<script type="text/javascript"
		src="${ctx}/static/js/jquery.easyui.min.js"></script>
	<script type="text/javascript"
		src="${ctx}/static/js/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="${ctx}/static/common/dateFormat.js"></script>

	<form action="" id="submitForm" method="post" style="display: none;">
		<input type="text" id="id" />
	</form>
	<form action="${ctx}/deployBill/selectDeployEmp" id="deployForm" method="post" style="display: none;">
		<input type="hidden" readonly  id="dworkBillId" name="workBillId" value="${workbill.id}" ></input>
	</form>
	<script type="text/javascript">
	$(function(){
		$("#visit_dialog").dialog('close');
		$("#assign_area_dialog").dialog('close');
		$("#assign_manager_dialog").dialog('close');
		
		
	});
	var ctx_val = $("#context_val").val();
	var datagrid;
	var rowEditor = undefined;
	function searchWorkBill() {
		var ctx_val = $("#context_val").val();

		var customerPhone = $("#customerPhone").val();
		var billStt = $('#billStt').combobox('getValue');
		var requestType = $('#requestType').combobox('getValue');
		var emergentType = $('#emergentType').combobox('getValue');
		 datagrid=$('#data').datagrid({
			title : '工单查询列表',
			iconCls : 'icon-edit',//图标  
			width : 700,
			height : 'auto',
			nowrap : false,
			striped : true,
			border : true,
			collapsible : false,//是否可折叠的  
			fit : true,//自动大小  
			url:ctx_val+"/workBill/selectWorkBillInfo",//加载的URL
			method:"post",
			 queryParams: { 
				 'customerPhone': customerPhone ,
				 'billStt':billStt,
				 'requestType':requestType,
				 'emergentType':emergentType,
			 },
			remoteSort : false,
			idField : 'fldId',
			singleSelect : true,//是否单选  
			pagination : true,//分页控件  
			rownumbers : true,//行号  
			frozenColumns : [ [
			                   

			{field:'deployId',title:'deployId',width:50,hidden:true},   
			{field:'empId',title:'empId',width:50,hidden:true},   
			{field:'roleNo',title:'roleNo',width:50,hidden:true},   
			{field:'roleName',title:'roleName',width:50,hidden:true},   
			{field:'roleId',title:'roleId',width:50,hidden:true},   
			{field:'id',title:'id',width:50,hidden:true},   
			 {field:'billNo',title:'工单号',width:100},   
			 {field:'customerPhone',title:'用户手机号',width:100},   
			 {field:'customerName',title:'用户姓名',width:100},
			 {field:'requestType',title:'工单类型',width:100,
					 formatter:function(value){
						 if(value==0){
							 return '维修';
						 } else if(value==1){
							 return '新装';   
						 } else if(value==2){
							 return '投诉';
						 }              
					 	
					 }  
			 },
			 {field:'billStt',title:'工单状态',width:150,
			 //状态，0待分派，1待处理，2（师傅回单后）待回访，3已完成，4退单（选择师傅退单？进入待处理状态），9作废
				 formatter:function(value){
						 if(value==0){
						 	return '待分派';      
						 }else if(value==1){
						 	return '待处理';        
						 }else if(value==2){
							 return '待回访';        
						 }else if(value==3){
						 	return '已完成';        
						 } else if(value==4){
						 	return '退单';        
						 } else if(value==6){
						 	return '转派/待接收';        
						 } 
						 else if(value==9){
						 	return '作废';        
						 }
				 }  
			 },
			 {field:'customerAdd',title:'用户住址',width:150},
			 {field:'issueDesc',title:'问题描述',width:150},
			 {field:'crtDate',title:'创建时间',width:150,formatter:
					function(value){
					if(value=="" || value == null)return"";
	            	return new Date(value).format("yyyy-MM-dd hh:mm:ss");
	            },sortable:true},
	            {field:'account',title:'当前处理人',width:100}
			] ],
			onRowContextMenu: function(e, rowIndex, rowData) {
				 e.preventDefault(); //阻止浏览器捕获右键事件
				 $(this).datagrid("clearSelections"); //取消所有选中项
				 $(this).datagrid("selectRow", rowIndex); //根据索引选中该行
				 $('#menu').menu('show', {
				 //显示右键菜单
				 left: e.pageX,//在鼠标点击处显示菜单
				 top: e.pageY
				 });
				 },
			toolbar : [ {
				text : '添加',
				iconCls : 'icon-add',
				handler : function() {
					var ctx_val = $("#context_val").val();
					$("#submitForm").attr("action", ctx_val+"/workBill/new" );
					$("#submitForm").attr("method", "get" );
					$("#submitForm").submit();
				}

			}, '-', {
				text : '修改',
				iconCls : 'icon-edit',
				handler : function() {
					var rows = datagrid.datagrid('getSelections');
					if(typeof(rows[0]) == "undefined"){
						window.alert("请选择一条要修改的数据");
						return false;
					}
					var id = rows[0].id;
					$("#id").val(id);
					
					var ctx_val = $("#context_val").val();
					$("#submitForm").attr("method", "post" );
					$("#submitForm").attr("action",
							ctx_val + "/workBill/viewBillById?id=" + id);
					$("#submitForm").submit();
				}

			},  ],

		});

		//设置分页控件  
		var p = $('#data').datagrid('getPager');
		$(p).pagination({
			pageSize : 10,//每页显示的记录条数，默认为10  
			pageList : [ 5, 10, 15 ],//可以设置每页记录条数的列表  
			beforePageText : '第',//页数文本框前显示的汉字  
			afterPageText : '页    共 {pages} 页',
			displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
		});
	}
	function deleteData(){
		var rows=datagrid.datagrid('getSelections');
		var iframeName = new Date().getTime();
		window.parent.addTabInSon("工单详情",iframeName)
		$("#dworkBillId").val(rows[0].id);
		$("#deployForm").attr("target",iframeName);
		$("#deployForm").submit();
	}

	$(function(){
		searchWorkBill();
	})

	function viewData() {
		var rows = datagrid.datagrid('getSelections');
		var ctx_val = $("#context_val").val();
		var id = rows[0].id;
		$("#id").val(id);
		var iframeName = new Date().getTime();
		window.parent.addTabInSon("工单详情",iframeName)
		$("#submitForm").attr("target",iframeName);
		$("#submitForm").attr("action",ctx_val + "/workBill/viewBillById?id=" + id);
		$("#submitForm").submit();

	}
	
	function assign() {
		var rows = datagrid.datagrid('getSelections');
		var selectData = rows[0];
		
		var billStt = selectData.billStt;
		if("0"==billStt){
			//待分派的状态
			var roleNo = selectData.roleNo;
			
			if(undefined==roleNo){
				//工单还未分派，直接生成派工单，派工单的当前处理人为区域主管。工单状态更新为6.
				BindProvinceCity();
				$("#assign_area_dialog").dialog('open');
			}else{
				
				window.alert("工单已分派。需要确定当前派工单的处理人角色");
			}
			return;
		}else if("6"==billStt){
			//需区域主管处理
			//查询区域主管下的所有维修员工
			$("#assign_manager_dialog").dialog('open');
			getManagedEmp();
			
			var account = selectData.account;
			$("#manager_id").val(account);
		}
		else{
			$.messager.alert('系统提示', "您选择的工单不是【待分派】状态，无法进行分派操作！", 'warning');
		}
		/**
		var ctx_val = $("#context_val").val();
		
		$("#id").val(id);
		$("#submitForm").attr("action",
				ctx_val + "/workBill/viewBillById?id=" + id);
		$("#submitForm").submit();
		**/
	}
	
	function telephoneVisit() {
		var rows = datagrid.datagrid('getSelections');
		var selectData = rows[0];
		
		if("2"==selectData.billStt){
			//window.alert("待回访");
			$("#visit_dialog").dialog({
				modal:true,	
			});
			
			$("#visit_billNo").val(selectData.billNo);
			$("#visit_customerPhone").val(selectData.customerPhone);
			$("#visit_issueDesc").val(selectData.issueDesc);
			
			$("#visit_dialog").dialog("open");
			return;
		}
		
	}
	
	function visitHander(){
		
		
		var rows = datagrid.datagrid('getSelections');
		var selectData = rows[0];
		$("#id").val(selectData.id);
		var billStt = $('#visit_result').combobox('getValue');
		 var _json = jQuery.param({ "billStt": billStt, "id": selectData.id });  
		 $("#visit_dialog").dialog("close");
		  $.ajax({  
		        url: ctx_val + "/workBill/workBillVisit",  
		        type: "POST",  
		        async: false,  
		        data: _json, //不能直接写成 {id:"123",code:"tomcat"}  
		        dataType: "json",  
		        // contentType: "charset=utf-8",  
		        cache: false,  
		        success: function (r, textStatus) {  
		            var result = r.result;
		            if("OK"==result){
		            	$.messager.alert('系统提示', "操作成功", 'info');
		            	searchWorkBill();
		            }else{
		            	$.messager.alert('系统提示', "操作失败，请稍后再试！", 'warning');
		            }
		        },  
		        error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.readyState); }  
		    });  

	}
	function area_AssignHander(){
		var rows = datagrid.datagrid('getSelections');
		var selectData = rows[0];
		
		var roleId = $('#area_emp').combobox('getValue');
		if(null==roleId || roleId.length==0){
			window.alert("请选择主管员工！");
			return false;
		}
		var billId = selectData.id;
		
		 var _json = jQuery.param({ "billId": billId, "roleId": roleId });  
		 $("#assign_area_dialog").dialog("close");
		  $.ajax({  
		        url: ctx_val + "/workBill/workBillAreaAssign",  
		        type: "POST",  
		        async: false,  
		        data: _json, //不能直接写成 {id:"123",code:"tomcat"}  
		        dataType: "json",  
		        // contentType: "charset=utf-8",  
		        cache: false,  
		        success: function (r, textStatus) {  
		            var result = r.result;
		            if("OK"==result){
		            	$.messager.alert('系统提示', "操作成功", 'info');
		            	searchWorkBill();
		            }else{
		            	$.messager.alert('系统提示', "操作失败，请稍后再试！", 'warning');
		            }
		        },  
		        error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.readyState); }  
		    });  
	}
	//绑定省份、城市、行政区信息
	function BindProvinceCity() {
		$.ajax({
			type: "POST",
			url:ctx_val+'/workBill/selectAreaInfo',
			dataType:"json",
			success: function(json){
			$("#assign_area").combobox({
				data:json.rows,
				valueField:'id',
				textField:'areaName',
				onChange: function (newValue, oldValue) {
		            $.get(ctx_val+'/workBill/selectEmpInfo', { areaId: newValue }, function (data) {
		            	if(data.rows.length ==0){
		            		$.messager.show({
								title:'错误',
								msg:'您选择的区域，没有找到对应的主管员工信息，无法进行分派',
								showType:'show'
						   });
		            		area_emp.combobox("clear");
		            	}else{
		            		area_emp.combobox("clear").combobox('loadData', data.rows);
		            	}
		            	
		               // district.combobox("clear");
		            }, 'json');
		        }
			});

			}

		});
		
		/**
	    var assign_area = $('#assign_area').combobox({
	        valueField: 'Value', //值字段
	        textField: 'Text', //显示的字段
	        url: ctx_val+'/workBill/selectAreaInfo',
	        editable: true,
	        
	        onChange: function (newValue, oldValue) {
	            $.get('/City/GetCitysByProvinceName', { provinceName: newValue }, function (data) {
	                city.combobox("clear").combobox('loadData', data);
	                district.combobox("clear")
	            }, 'json');
	        }
	    });
	        **/
	   
	    var area_emp = $('#area_emp').combobox({
	        valueField: 'id', //值字段
	        textField: 'account', //显示的字段
	        editable: true,
	        onChange: function (newValue, oldValue) {
	        	/**
	            $.get('/District/GetDistrictByCityName', { cityName: newValue }, function (data) {
	                district.combobox("clear").combobox('loadData', data);
	            }, 'json');
	        	*/
	        }
	    });
		
	        /*
	    var district = $('#District').combobox({
	        valueField: 'Value', //值字段
	        textField: 'Text', //显示的字段
	        editable: true
	    });
	    
	    */
	}
	function empInit(){
		$.ajax({
			
			url:ctx_val+'/workBill/selectEmpInfo',
			dataType:"json",
			type: "POST",
		    async: false,  
	        data: _json, //不能直接写成 {id:"123",code:"tomcat"}  
	        dataType: "json",  
	        cache: false,  
			success: function(json){
			$("#assign_area").combobox({
				data:json.rows,
				valueField:'id',
				textField:'areaName',
				onChange: function (newValue, oldValue) {
					//window.alert("test:"+newValue+"  "+oldValue);
		           
		        }
			});

			}

		});
	}
	
	function getManagedEmp(){
		var rows = datagrid.datagrid('getSelections');
		var selectData = rows[0];
		
		var empId = selectData.empId;
		 var _json = jQuery.param({ "parentId": empId });  
		$.ajax({
			type: "POST",
			data: _json, 
			url:ctx_val+'/workBill/getManagedEmp',
			dataType:"json",
			success: function(json){
				$("#managed_emp").combobox({
					data:json.rows,
					valueField:'id',
					textField:'empName',
				});

			}

		});
	}
	
	/*主管分派*/
	function manager_AssignHander(){
		var rows = datagrid.datagrid('getSelections');
		var selectData = rows[0];
		
		var deployId = selectData.deployId;
		var empId = $('#managed_emp').combobox('getValue');
		
		var workBillId =  selectData.id;
		var _json = jQuery.param({ "deployId": deployId, "empId": empId ,"workBillId":workBillId});  
		 $("#assign_manager_dialog").dialog("close");
		  $.ajax({  
		        url: ctx_val + "/workBill/managedEmpAssign",  
		        type: "POST",  
		        async: false,  
		        data: _json, //不能直接写成 {id:"123",code:"tomcat"}  
		        dataType: "json",  
		        // contentType: "charset=utf-8",  
		        cache: false,  
		        success: function (r, textStatus) {  
		            var result = r.result;
		            if("OK"==result){
		            	$.messager.alert('系统提示', "操作成功", 'info');
		            	searchWorkBill();
		            }else{
		            	$.messager.alert('系统提示', "操作失败，请稍后再试！", 'warning');
		            }
		        },  
		        error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.readyState); }  
		    });  
		
	}
</script>

</body>
</html>